<?php

require_once(WSB_MODULES_PATH .'sherpa/models/BasketProduct.php');

class Sherpa_Model_BasketProducts extends WSB_Model_Collection
{
	/**
	 * @var Sherpa_Model_Product
	 */
	protected $_modelClass = 'Sherpa_Model_BasketProduct';

	/**
	 * @return array
	 */
	public function getParentProductIds()
	{
		$parentProductIds = array();
		foreach($this AS $basketProduct) {
			$parentProductId = $basketProduct->getParentProductId();
			$parentProductIds[$parentProductId] = $parentProductId;
		}
		return array_values($parentProductIds);
	}

	/**
	 * @return array
	 */
	public function getProductIds()
	{
		$productIds = array();
		foreach($this AS $basketProduct) {
			$productId = $basketProduct->getProductId();
			$productIds[$productId] = $productId;
		}
		return array_values($productIds);
	}

	/**
	 * @param integer $languageId
	 */
	public function loadMainImages($languageId)
	{
		// Bilder holen und mit Produkten zusammenf�hren
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageGateway.php');
		$gateway = new Sherpa_Model_ImageGateway();
		$images = $gateway->fetchParentProductMainImages($this->getParentProductIds(), $languageId);
		$this->_mergeImages($images);
	}

	/**
	 * @param array $images
	 */
	private function _mergeImages(array $images)
	{
		foreach($images AS $image) {
			$parentProductId = $image['parentProductId'];
			foreach($this->_data AS $index=>$data) {
				if($data['parentProductId'] == $parentProductId) {
					$this->_data[$index]['image'] = $image;
				}
			}
		}
	}

	/**
	 * @param integer $languageId
	 */
	public function loadImageVariants()
	{
		// Bildvarianten holen und mit Produkten zusammenf�hren
		require_once(WSB_MODULES_PATH .'sherpa/models/ImageGateway.php');
		$gateway = new Sherpa_Model_ImageGateway();
		$imageVariants = $gateway->fetchParentProductImageVariants($this->getParentProductIds());
		$this->_mergeImageVariants($imageVariants);
	}

	/**
	 * @param array $images
	 */
	private function _mergeImageVariants(array $imageVariants)
	{
		foreach($imageVariants AS $imageVariant) {
			$parentProductId = $imageVariant['parentProductId'];
			$colorApplicationId = $imageVariant['colorApplicationId'];
			foreach($this->_data AS $index=>$data) {
				if($data['parentProductId'] == $parentProductId) {
					if($data['colorApplicationId'] == $colorApplicationId) {
						$this->_data[$index]['imageVariant'] = $imageVariant;
					}
				}
			}
		}
	}

	/**
	 * @return void
	 */
	public function loadColorApplications()
	{
		require_once(WSB_MODULES_PATH .'sherpa/models/ColorApplicationGateway.php');
		$gateway = new Sherpa_Model_ColorApplicationGateway();
		$colorApplications = $gateway->fetchChildProductColorApplications($this->getProductIds());
		$this->_mergeColorApplications($colorApplications);
	}

	/**
	 * @param array $colorApplications
	 */
	private function _mergeColorApplications(array $colorApplications)
	{
		foreach($colorApplications AS $colorApplication) {
			$colorApplicationId = $colorApplication['id'];
			foreach($this->_data AS $index=>$data) {
				if($data['colorApplicationId'] == $colorApplicationId) {
					$this->_data[$index]['colorApplication'] = $colorApplication;
				}
			}
		}
	}

	/**
	 * @return float
	 */
	public function calculateSubtotal()
	{
		$total = 0;
		foreach($this AS $price) {
			$total += $price->getPriceTotal();
		}
		return $total;
	}

	/**
	 * @param Sherpa_Model_Coupon $coupon
	 * @return float
	 */
	public function calculateTotal(Sherpa_Model_Coupon $coupon)
	{
		$subtotal = $this->calculateSubtotal();

		$hooks = WSB_Hooks::instance();
		$hooks->execute('Sherpa/Model/BasketProducts/CalculateTotal', $subtotal, $this);

		$couponValue = $coupon->getValueRemaining();
		$shippingCosts = $this->calculateShippingCosts($coupon);
		return $subtotal - $couponValue + $shippingCosts;
	}

	/**
	 * @param Sherpa_Model_Coupon $coupon
	 * @return float
	 */
	public function calculateShippingCosts(Sherpa_Model_Coupon $coupon)
	{
		return 8.50;
	}

}
